package nancal.mp.entity.base

import nbcp.base.extend.*
import nbcp.base.json.*
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.annotation.ComponentScan
import org.springframework.test.context.junit.jupiter.SpringExtension
import java.io.File
import java.io.FileWriter
import java.time.Duration

/**
 * Created by udi on 17-3-27.
 */

@SpringBootApplication(exclude = arrayOf(
    DataSourceAutoConfiguration::class,
    MongoAutoConfiguration::class,
    RedisAutoConfiguration::class,
    ElasticsearchDataAutoConfiguration::class
))
@ExtendWith(SpringExtension::class)
@SpringBootTest(classes= arrayOf(TestBase::class))
@ComponentScan("nancal.mp.entity.*")
abstract class TestBase {
    var workPath = System.getProperty("user.dir")

    val entityProjectName = "mp-entity"
    val ormProjectName = "mp-orm"
    val entityPkgName = "nancal.mp"
    val ormPkgName = "nancal.mp"

    val entityPkgPath :String
        get() = entityPkgName.replace('.', '/')
    val ormPkgPath :String
        get() = ormPkgName.replace('.', '/')

    init {
//        GenericXmlApplicationContext().environment.setActiveProfiles("test")

//        println(SpringUtil.context.environment.activeProfiles)
        //ParserConfig.getGlobalInstance().putDeserializer(ObjectId::class.java, ObjectIdDeserializer())
    }


    fun execTimes(name: String, times: Int, func: (Int) -> String) {
        println("${name} 执行1000次:")
        var startAt = System.currentTimeMillis()
        for (i in 1..times) {
            var ret = func(i)
            if (ret.isEmpty()) continue

            println(ret)
        }
        var endAt = System.currentTimeMillis()

        println(Duration.ofMillis(endAt - startAt).toSummary())
    }


    fun writeToFile(fileName: String, content: String) {
        File(fileName).parentFile.mkdirs()
        FileWriter(fileName, false)
            .use { moer_File ->
                moer_File.appendLine(content)
                moer_File.flush()
            }
    }




}


